﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UI.WebPage.Background;
using Model.AuthModel;
using BLL.AuthModel;
namespace Com.Ompchina.Web.admin.system
{
    public partial class add_roleauth : AddInfoPage, IAddInfo
    {
        protected RolesInfo _DefaultRoles = new RolesInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.InitSingleInfo(this.GetUpdateID());
            if (!IsPostBack) 
            {
                this.InitModuleType();
            }
            if (!IsPostBack && PageAction.Equals("update")) 
            { 
                this.InitRolesAuth(); 
            }
        }

        #region 私有方法
        /// <summary>
        /// 绑定模块分类列表
        /// </summary>
        private void InitModuleType()
        {
            IList<ModuleTypeInfo> list = new ModuleTypeBLL().ListAllModuleType();
            rptModuleType.DataSource = list;
            rptModuleType.DataBind();
        }
        /// <summary>
        /// Repeat嵌套控件绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptModuleType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ModuleTypeInfo info = (ModuleTypeInfo)e.Item.DataItem;
                //根据分类编号，获取权限
                IList<AuthInfo> list = new AuthBLL().ListAuthByModuleType(info.ModuleTypeID);
                Repeater rptModule = (Repeater)e.Item.FindControl("rptModule");
                if (rptModule != null)
                {
                    rptModule.DataSource = list;
                    rptModule.DataBind();
                }
            }
        }
        /// <summary>
        /// 初始化角色拥有的权限
        /// </summary>
        private void InitRolesAuth() 
        {
            HiddenField hf = null;
            CheckBox cb = null;
            Repeater rpt = null;
            IList<RolesInfo> list = new RolesBLL().ListAuthByRoles(_DefaultRoles.RolesID);
            foreach (RepeaterItem item in rptModuleType.Items)
            {
                rpt = (Repeater)item.FindControl("rptModule");
                if (rpt != null)
                {
                    foreach (RepeaterItem mitem in rpt.Items)
                    {
                        cb = (CheckBox)mitem.FindControl("chkTake");
                        hf = (HiddenField)mitem.FindControl("hfAuth");
                        if (list != null && list.Count > 0)
                        {
                            foreach (RolesInfo info in list)
                            {
                                if (hf.Value.Equals(info.RolesAuth.Auth.AuthID.ToString()))
                                    cb.Checked = true;
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// 获取选择的权限
        /// </summary>
        /// <returns></returns>
        private string GetAuth()
        {
            string result = string.Empty;
            HiddenField hf = null;
            CheckBox cb = null;
            Repeater rpt = null;
            foreach (RepeaterItem item in rptModuleType.Items)
            {
                rpt = (Repeater)item.FindControl("rptModule");
                if (rpt != null)
                {
                    foreach (RepeaterItem mitem in rpt.Items)
                    {
                        cb = (CheckBox)mitem.FindControl("chkTake");
                        hf = (HiddenField)mitem.FindControl("hfAuth");
                        if (cb.Checked)
                        {
                            if (string.IsNullOrEmpty(result))
                                result = hf.Value;
                            else
                                result += "," + hf.Value;
                        }
                    }
                }
            }
            return result;
        }
        /// <summary>
        /// 保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            RolesAuthBLL bll = new RolesAuthBLL();
            if (bll.UpdateRoleAuth(_DefaultRoles.RolesID, this.GetAuth()))
            {
                base.SetMessage(true, "授权成功");
                base.Redirect("list_role.aspx?action=list", 1);
            }
            else
                base.SetMessage(false, "授权失败，请重试");
        }        
        #endregion


        #region IAddPage 成员
        public bool CheckInput()
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 初始化角色信息
        /// </summary>
        /// <param name="id"></param>
        public void InitSingleInfo(int id)
        {
            if (id > 0) 
            {
                _DefaultRoles = new RolesBLL().GetRolesByID(id);
                if (_DefaultRoles.RolesID > 0) 
                {                    
                    hfID.Value = id.ToString();
                }
            }
        }
        #endregion        
    }
}
